package com.taikang.dao;

import com.taikang.dao.base.DefaultCommonDaoImpl;
import com.taikang.entity.Employee;
import com.taikang.entity.SalaryRecord;
import com.taikang.entity.TaskRecord;
import com.taikang.entity.TrainCourse;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.stereotype.Repository;

/**
 * Created by zhai on 16/5/10.
 */
@Repository
public class EmployeeDao extends DefaultCommonDaoImpl<Employee> {

    private SqlCallback trainPojoSqlCallback = null;
    private SqlCallback trainListSqlCallback = null;

    public Employee employeeLogin(String username, String password) {
        return queryForOne(Cnd.where("LoginName", "=", username).and("Password", "=", password));
    }

    public Object getSalaryByEmployeeId(Integer employeeId, Integer recordId, Integer page, Integer size) {
        if (null == recordId) {
            return listPage(page, size, Cnd.where("EmployeeID", "=", employeeId), SalaryRecord.class);
        } else {
            return fetch(SalaryRecord.class, recordId);
        }
    }

    public Object getTrainInfo(Integer agentId, Integer courseId, Integer page, Integer size) {
        Sql sql = Sqls.create(getDao().sqls().get("getTrainInfo"));
        Criteria cri = Cnd.cri();
        cri.where().and("a.AgentId", "=", agentId);
        if (null != courseId) {
            cri.where().and("a.CourseId", "=", courseId);
        }
        SqlCallback callback = null;
        sql.setCondition(cri);
        if (null == courseId) {
            if (null == trainListSqlCallback)
                trainListSqlCallback = createSqlCallBack(true, TrainCourse.class);
            callback = trainListSqlCallback;
        } else {
            if (null == trainPojoSqlCallback)
                trainPojoSqlCallback = createSqlCallBack(false, TrainCourse.class);
            callback = trainPojoSqlCallback;
        }
        sql.setCallback(callback);
        return null == courseId ? listPage(page, size, sql) : ((Sql) execute(sql)).getContext().getResult();
    }

    public Object getTaskInfo(Integer employeeId, Integer taskId, Integer page, Integer size) {
        if (null == taskId) {
            return listPage(page, size, Cnd.where("EmployeeID", "=", employeeId), TaskRecord.class);
        } else {
            return fetch(TaskRecord.class, taskId);
        }
    }
}
